package com.github.live.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.widget.TextView;

import com.github.live.R;

import java.util.List;

/**
 * @author pj567
 * @date :2021/1/12
 * @description:
 */
public class LiveSettingAdapter extends SimpleRecyclerAdapter<LiveSettingItem> {
    private int activeIndex = -1;
    private int selectIndex = -1;
    @SuppressLint("ResourceType")
    public LiveSettingAdapter() {
        super(R.layout.item_live_setting);
    }

    public void setSelectedIndex(int index) {
        this.selectIndex = index;
    }
    public int getSelectedIndex() {
        return this.selectIndex;
    }

    public void setSingleActiveIndex(int index) {
        if (activeIndex == index) {
            return;
        }
        if (activeIndex >= 0 && activeIndex < getItemCount()) {
            getItem(activeIndex).cancel();
            notifyItemChanged(activeIndex);
        }
        this.activeIndex = index;
        getItem(index).selected();
        notifyItemChanged(index);
    }

    @Override
    public void setData(List<LiveSettingItem> mData) {
        if (this.activeIndex != -1) {
            getItem(this.activeIndex).cancel();
            this.activeIndex = -1;
        }
        super.setData(mData);
    }

    public void setMultiActiveIndex(int index, boolean selected) {
        getItem(index).setSelected(selected);
        notifyItemChanged(index);
    }

    @Override
    void onBindViewHolder(SimpleViewHolder holder, LiveSettingItem settingItem, int position) {
        TextView tvGroupName = holder.findViewById(R.id.tvSettingItemName);
        tvGroupName.setText(settingItem.getName());
        if (settingItem.isSelected()) {
            tvGroupName.setTextColor(holder.getView().getResources().getColor(R.color.color_theme));
        } else {
            tvGroupName.setTextColor(Color.WHITE);
        }
    }
}